package com.soujiayi.adapter;

import java.util.List;
import com.soujiayi.activity.R;
import com.soujiayi.model.MyFavorite;
import com.soujiayi.model.Product;
import com.soujiayi.net.ImageLoader;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyFavoriteListAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private List<Product> data;
	private ImageLoader imageLoader;
	private String mallNumberStr = "";
	private int width;//屏宽
	private int height;//屏高

	public MyFavoriteListAdapter(Context context,
			List<Product> data) {
		this.data = data;
		this.mInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader=new ImageLoader(context.getApplicationContext(),true,100,100,2);
		
		this.mallNumberStr = context.getResources().getString(R.string.mall_number);
		
		DisplayMetrics dm = new DisplayMetrics();  
		dm = context.getResources().getDisplayMetrics();
		width = (int) (100*dm.density);
		height = (int) (100*dm.density);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		
		View view = convertView;
		ViewHolder holder; 
		if(view == null){
			view = mInflater.inflate(R.layout.my_favorite_list_item, null);
			holder = new ViewHolder();
			holder.favImg = (ImageView)view.findViewById(R.id.iv_favorite_item);
			holder.favPrice = (TextView)view.findViewById(R.id.tv_favorite_price);
			holder.favName = (TextView)view.findViewById(R.id.tv_favorite_name);
			holder.favMall = (TextView)view.findViewById(R.id.tv_favorite_mall);
			holder.favMallCount = (TextView)view.findViewById(R.id.tv_favorite_mallCount);
			view.setTag(holder);
		}else{
			holder = (ViewHolder)view.getTag(); 
		}
		Product item = data.get(position);
		bindView(holder, item);

		return view;
	}

	private void bindView(ViewHolder holder, Product item) {
		String favName = item.getName();
		if(favName != null){
			if(favName.length() > 50){
				favName = favName.substring(0, 50) + "...";
			}
		}
		holder.favName.setText(favName == null ? "" : favName);
		String favPrice = item.getSaleprice();
		holder.favPrice.setText(favPrice == null ? "" : favPrice);


		int mallCount = (item.getCompare()==null)?0:item.getCompare().getMallnum();
		
		if(mallCount != 0 ){
			holder.favMallCount.setText(mallCount + mallNumberStr);
			holder.favMall.setText("");
		}else{
			holder.favMallCount.setText("");
			String favMall = item.getMallname();
			holder.favMall.setText(favMall == null ? "" : favMall);
		}
		
		holder.favImg.setDrawingCacheEnabled(true);
		if(item.getCover() != null){
			String imageUrl = item.getCover().replace(".jpg","." + width + "x" + height + ".jpg");
			imageLoader.DisplayImage(imageUrl,holder.favImg);
		}
	}

	static class ViewHolder {
		TextView favName, favPrice, favMall, favMallCount;
		ImageView favImg;
	}
	
	public void close(){
		imageLoader.close();
	}

}
